package com.dtf.base.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dtf.base.common.utils.Const;
import com.dtf.base.common.vo.Pagination;
import com.dtf.base.demo.service.DemoService;
import com.dtf.base.demo.vo.Demo;


@Controller
@RequestMapping("/base/demo")
public class DemoController {
	/**
	 * /base/demo/demo?userName=张三
	 * @param httpSession
	 * @param request
	 * @param response
	 * @param userName
	 * @return
	 */
	@RequestMapping("demo")
	public String demo(HttpSession httpSession,HttpServletRequest request, HttpServletResponse response,
			String userName) {
		//如果出现中文乱码
		//把Tomcat的conf目录下的server.xml中配置 Connector的URIEncoding="UTF-8" 属性即可
		request.setAttribute("str", "hi,"+userName);
		return "base/base";
	}
	
	/**
	 * /base/demo/list
	 * 用fastjson直接返回json字段串
	 * @return
	 */
	@ResponseBody
	@RequestMapping("list")
	public List list() {
		List t = new ArrayList();
		Map m1 = new HashMap();
		m1.put("a", "测试");
		t.add(m1);
		return t;
	}
	
	/**
	 * /base/demo/liststr
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="liststr",produces="text/json;charset=UTF-8")
	public String liststr() {
		List t = new ArrayList();
		Map m1 = new HashMap();
		m1.put("a", "测试");
		t.add(m1);
		
		return JSON.toJSONString(t);
	}
	
	/**
	 * 将ID通过路径传过来
	 * /base/demo/pathParam/12333
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="pathParam/{id}")
	public String pathParam(@PathVariable String id) {
		
		return id;
	}
	
	@Resource
	private DemoService demoService;
	
	@ResponseBody
	@RequestMapping(value="queryList")
	public List queryList(){
		
		return demoService.queryList(null);
	}
	
	@ResponseBody
	@RequestMapping(value="queryPage")
	public Pagination queryPage(){
		Map param = new HashMap();
		return demoService.queryPage(param);
	}
	
	@ResponseBody
	@RequestMapping(value="save")
	public Map save(String demo_name) throws Exception{
		Map p = new HashMap();
		p.put("demo_name", demo_name);
		demoService.save(p);
		Map req = new HashMap();
		req.put("msg", "成功");
		return req;
	}
	
	@ResponseBody
	@RequestMapping(value="saveBean")
	public Map saveBean(String demo_name) throws Exception{
		Demo demo = new Demo();
		demo.setDemo_id(Const.getUUID());
		demo.setDemo_name(demo_name);
		int r = demoService.add(demo);
		Map req = new HashMap();
		req.put("msg", "成功:"+r);
		return req;
	}
	
	@ResponseBody
	@RequestMapping(value="editBean")
	public Map editBean(String demo_name,String demo_id,String sex) throws Exception{
		Demo demo = new Demo();
		
		//根据ID修改
		//http://localhost:8080/spring-mvc/base/demo/editBean?demo_id=123&demo_name=张三&sex=男
//		demo.setDemo_id(demo_id);
//		demo.setDemo_name(demo_name);
//		demo.setSex(sex);
//		int r = demoService.editWithColumn(demo, "demo_name");
		
		//	根据其它字段修改
		//http://localhost:8080/spring-mvc/base/demo/editBean?demo_name=张三&sex=男
		demo.setDemo_name(demo_name);
		demo.setSex(sex);
		int r = demoService.editWithColumn(demo, "demo_name");
		Map req = new HashMap();
		req.put("msg", "成功:"+r);
		return req;
	}
}
